package com.xhj.sportsnews.adapter;

import android.databinding.BindingAdapter;
import android.media.MediaPlayer;
import android.view.SurfaceView;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.squareup.picasso.Picasso;
import com.xhj.sportsnews.R;

import java.io.IOException;
import java.util.List;

/**
 * Created by XHJ on 2016/12/17.
 */
public class HeadRecyclerImgBindngs {
    private static String url;
    private static SurfaceView view1;
    private static ImageView iv1;
    private static ImageView iv2;
    @BindingAdapter("loadUrl")
    public static void loadImage(ImageView view, String url){
        Picasso.with(view.getContext())
                .load(url)
                .into(view);

    }

    @BindingAdapter("showLogo")
    public static void showLogo(RelativeLayout layout,String url){
        if (url==null){
            layout.setVisibility(View.GONE);
        }
    }
    @BindingAdapter("hideImage")
    public static void hideImage(ImageView view,String url){

        if (url == ""){
            view.setVisibility(View.INVISIBLE);
        }
    }
    @BindingAdapter("hidevideoImage")
    public static void hidevideoImage(ImageView view,boolean flag){
        iv1=view;
        if(flag)
            view.setVisibility(View.INVISIBLE);

    }
    @BindingAdapter("hidevideoImage2")
    public static void hidevideoImage2(ImageView view,boolean flag){
        iv2=view;
        if(flag)
            view.setVisibility(View.INVISIBLE);

    }

    @BindingAdapter("showGridView")
    public static void showGridView(GridView view, List<String> images) {
        if (images != null) {
            GridViewAdapter adapter = new GridViewAdapter(images, view.getContext());
            view.setAdapter(adapter);
        }
    }

    @BindingAdapter("showVideo")
    public static void showVideo(SurfaceView view, String video){
         url = video;
         view1= (SurfaceView) view.findViewById(R.id.item_video);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MediaPlayer player = new MediaPlayer();
                player.setDisplay(view1.getHolder());

                try {
                    if(url!=null) {
                        player.setDataSource(url);
                        player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                            @Override
                            public void onPrepared(MediaPlayer mediaPlayer) {
                                hidevideoImage(iv1,true);
                                hidevideoImage(iv2,true);
                                mediaPlayer.start();
                            }
                        });
                        player.prepareAsync();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });




    }
}
